﻿using System.Windows;
using System.Windows.Controls;
using Microsoft.Xaml.Behaviors;

namespace Base.ModuleBase.Behaviors
{
    public class EditableTextBlockBehavior:Behavior<TextBlock>
    {
        //private  Style _editingTextBlockStyle;
        //private  Style EditingTextBlockStyle
        //{
        //    get
        //    {
        //        if (_editingTextBlockStyle == null)
        //        {
        //            var dic=new ResourceDictionary();
        //            dic.Source=new Uri("pack://application:,,,/Base.ModuleBase;component/Behaviors/EditableTextBlockBehavior.xaml", UriKind.RelativeOrAbsolute);
        //            _editingTextBlockStyle=dic["_EditingTextBlockStyle"] as Style;
        //        }
        //        return _editingTextBlockStyle;
        //    }
        //}
        public bool IsEditing
        {
            get { return (bool)GetValue(IsEditingProperty); }
            set { SetValue(IsEditingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsEditing.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsEditingProperty =
            DependencyProperty.Register("IsEditing", typeof(bool), typeof(EditableTextBlockBehavior), new PropertyMetadata(false,IsEditingChanged));

        private static void IsEditingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var bhv = d as EditableTextBlockBehavior;
            if (bhv == null) return;
            bhv.OnIsEditingChanged();
        }

        protected override void OnAttached()
        {
            base.OnAttached();
        }

        private void OnIsEditingChanged()
        {
            if (IsEditing)
            {
         //       AssociatedObject.Style = EditingTextBlockStyle;
            }
        }

    }
}
